<script setup lang="ts">
import { useCountDown } from '@/hooks'
import { OrderStatus } from '@/constants'
import type { OrderItem } from '@/types'
import { toRefs } from 'vue'

interface Props {
  orderItem: OrderItem
}
const props = defineProps<Props>()
const { orderItem } = toRefs(props)
const { countTimeText, start } = useCountDown()
if (orderItem.value.countdown !== -1) {
  start(orderItem.value.countdown)
}
</script>

<template>
  <div class="order-item">
    <div class="head">
      <span>下单时间：{{ orderItem.createTime }}</span>
      <span>订单编号：{{ orderItem.id }}</span>
      <span class="down-time" v-if="orderItem.countdown !== -1 && orderItem.orderState === 1">
        <i class="iconfont icon-down-time"></i>
        <b>付款截止：{{ countTimeText }}</b>
      </span>
    </div>
    <div class="body">
      <div class="column goods">
        <ul>
          <li v-for="item in orderItem.skus" :key="item.id">
            <RouterLink class="image" :to="`/goods/${item.spuId}`">
              <img :src="item.image" />
            </RouterLink>
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="attr ellipsis">
                <span>{{ item.attrsText }}</span>
              </p>
            </div>
            <div class="price">¥{{ item.realPay }}</div>
            <div class="count">x{{ item.quantity }}</div>
          </li>
        </ul>
      </div>
      <div class="column state">
        <p>{{ OrderStatus[orderItem.orderState].label }}</p>
        <p v-if="orderItem.orderState === 3">
          <a href="javascript:;" class="green">查看物流</a>
        </p>
        <p v-if="orderItem.orderState === 4">
          <a href="javascript:;" class="green">评价商品</a>
        </p>
        <p v-if="orderItem.orderState === 5">
          <a href="javascript:;" class="green">查看评价</a>
        </p>
      </div>
      <div class="column amount">
        <p class="red">¥{{ orderItem.payMoney }}</p>
        <p>（含运费：¥{{ orderItem.postFee }}）</p>
        <p>在线支付</p>
      </div>
      <div class="column action">
        <XtxButton
          v-if="orderItem.orderState === 1"
          type="primary"
          size="small"
          @click="$router.push(`/member/pay/${orderItem.id}`)"
        >
          立即付款
        </XtxButton>
        <XtxButton v-if="orderItem.orderState === 3" type="primary" size="small">
          确认收货
        </XtxButton>
        <p><a href="javascript:;">查看详情</a></p>
        <p v-if="[2, 3, 4, 5].includes(orderItem.orderState)">
          <a href="javascript:;">再次购买</a>
        </p>
        <p v-if="[4, 5].includes(orderItem.orderState)">
          <a href="javascript:;">申请售后</a>
        </p>
        <p v-if="orderItem.orderState === 1"><a href="javascript:;">取消订单</a></p>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;
}

.head {
  padding: 0 20px;
  height: 50px;
  line-height: 50px;
  background: #f5f5f5;
  overflow: hidden;

  span {
    margin-right: 20px;

    &.down-time {
      float: right;
      margin-right: 0;

      i {
        margin-right: 3px;
        vertical-align: middle;
      }

      b {
        font-weight: normal;
        vertical-align: middle;
      }
    }
  }

  .del {
    float: right;
    margin-right: 0;
    color: #999;
  }
}

.body {
  display: flex;
  align-items: stretch;

  .column {
    padding: 20px;
    text-align: center;
    border-left: 1px solid #f5f5f5;

    > p {
      padding-top: 10px;
    }

    &:first-child {
      border-left: none;
    }

    &.goods {
      flex: 1;
      align-self: center;
      padding: 0;

      ul {
        li {
          display: flex;
          padding: 10px;
          border-bottom: 1px solid #f5f5f5;

          &:last-child {
            border-bottom: none;
          }

          .image {
            width: 70px;
            height: 70px;
            border: 1px solid #f5f5f5;
          }

          .info {
            padding: 0 10px;
            width: 220px;
            text-align: left;

            p {
              margin-bottom: 5px;

              &.name {
                height: 38px;
              }

              &.attr {
                font-size: 12px;
                color: #999;

                span {
                  margin-right: 5px;
                }
              }
            }
          }

          .price {
            width: 100px;
          }

          .count {
            width: 80px;
          }
        }
      }
    }

    &.state {
      width: 120px;

      .green {
        color: @xtxColor;
      }
    }

    &.amount {
      width: 200px;

      .red {
        color: @priceColor;
      }
    }

    &.action {
      width: 140px;

      a {
        display: block;

        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
